package com.tzb.product.controller;

import com.tzb.product.entity.Product;
import com.tzb.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Value("${server.port}")
    private String port;

    @Value("${spring.cloud.client.ip-address}")
    private String ip;

    @GetMapping("/{id}")
    public Product findById(@PathVariable Long id) {

//        try {
//            Thread.sleep(2000);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        Product product = productService.findById(id);
        product.setProductName("访问的服务地址：" + ip + ":" + port);
        return product;
    }

    @PostMapping
    public void save(@RequestBody Product product) {
        productService.save(product);
        System.out.println("保存成功");
    }

}
